package com.bjyhl.core.net.callback;

import android.util.Log;

import com.bjyhl.core.ui.loader.FragmentLoader;
import com.bjyhl.core.ui.loader.LoaderStyle;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RequestCallbacks implements Callback<String> {

    private final IRequest REQUEST;
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IError ERROR;
    private final LoaderStyle LOADER_STYLE;
//    private static final Handler HANDLER = new Handler();

    public RequestCallbacks(IRequest request, ISuccess success, IFailure failure, IError error, LoaderStyle loaderStyle) {
        this.REQUEST = request;
        this.SUCCESS = success;
        this.FAILURE = failure;
        this.ERROR = error;
        this.LOADER_STYLE = loaderStyle;
    }

    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        if (response.isSuccessful()) {
            if (call.isExecuted()) {
                if (SUCCESS != null) {
                    SUCCESS.onSuccess(response.body());
                }
            }
        } else {
            if (ERROR != null) {
                ERROR.onError(response.code(), response.message());
            }
        }
        stopLoading();
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        Log.e("maybe upload", "onFailure: " + t.getMessage());
        // lib add 转发错误处理
        if(ERROR != null) {
            ERROR.onError(-1, t.getMessage());
        }
//        if (FAILURE != null) {
//            FAILURE.onFailure();
//        }
        if (REQUEST != null) {
            REQUEST.onRequestEnd();
        }
        stopLoading();
    }

    private void stopLoading() {
        if (LOADER_STYLE != null) {
            FragmentLoader.stopLoading();
//            if (LOADER_STYLE != null) {
//                HANDLER.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        FragmentLoader.stopLoading();
//                    }
//                }, 2000);
//            }
        }
    }
}
